package org.richin.lang.util;

import java.text.MessageFormat;

public final class NullUtils
{
    /**
     * The default constructor is made private because instances of this class should
     * not be created.
     */
    private NullUtils()
    {
    }

    /**
     * Verifies if two objects are equal, by considering two <code>null</code> values
     * to be equal, a <code>null</code> and non-<code>null</code> value not to be
     * equal, and by using the <code>equals()</code> method to compare two
     * non-<code>null</code> values.
     *
     * @param p_first the first object to consider for equality.
     * @param p_second the second object to consider for equality.
     *
     * @return <code>true</code> if the two objects are considered equal,
     * <code>false</code> otherwise.
     */
    public static boolean safeEquals(Object p_first, Object p_second)
    {
        boolean result = false;

        if (p_first == null)
        {
            result = (p_second == null);
        }
        else if (p_second != null)
        {
            result = p_first.equals(p_second);
        }
        return result;
    }

    /**
     * Verifies if two arrays of objects are equal, considering <code>null</code>
     * values to be equal, and comparing corresponding elements of each array.
     *
     * @param p_first the first array to consider for equality.
     * @param p_second the second array to consider for equality.
     *
     * @return <code>true</code> if the two arrays are considered equal,
     * <code>false</code> otherwise.
     */
    public static boolean safeEqualArrays(Object[] p_first, Object[] p_second)
    {
        boolean result = false;

        if (p_first == null)
        {
            result = (p_second == null);
        }
        else if (p_second != null)
        {
            int length1 = p_first.length;
            int length2 = p_second.length;

            if (length1 == length2)
            {
                result = true;

                for (int i = 0; i < length1; i++)
                {
                    if (!safeEquals(p_first[i], p_second[i]))
                    {
                        result = false;
                        break;
                    }
                }
            }
        }
        return result;
    }

    /**
     * Returns the result of comparing two objects, by considering two
     * <code>null</code> values to be equal, a <code>null</code> to come after a
     * non-<code>null</code> value, and by using the <code>compareTo()</code> method
     * to compare two non-<code>null</code> values.
     *
     * @param p_first the first object to consider for comparison.
     * @param p_second the second object to consider for comparison.
     *
     * @return -1 if the first object should come before the second, 1 if it is vice
     * versa, and 0 if the two objects are considered equal.
     */
    public static <T> int safeCompare(Comparable<T> p_first, T p_second)
    {
        int result = -1;

        if (p_first == null)
        {
            result = (p_second == null) ? 0 : 1;
        }
        else if (p_second != null)
        {
            result = p_first.compareTo(p_second);
        }
        return result;
    }

    /**
     * Checks an object, returning it if it is not <code>null</code>, returning a
     * default value otherwise.
     *
     * @param p_object a given object.
     * @param p_default the default value in case the given object is
     * <code>null</code>.
     *
     * @return the given object if it is not <code>null</code>. If it is, the
     * default value is returned instead.
     */
    public static Object getDefault(Object p_object, Object p_default)
    {
        if (p_object != null)
        {
            return p_object;
        }
        return p_default;
    }

}
